// reg_user.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "reg_user.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// The one and only application object

CWinApp theApp;

using namespace std;

bool FileExist( LPCTSTR filename )
{
	// Data structure for FindFirstFile
	WIN32_FIND_DATA findData;

	// Clear find structure
	ZeroMemory(&findData, sizeof(findData));

	// Search the file
	HANDLE hFind = FindFirstFile( filename, &findData );
	if ( hFind == INVALID_HANDLE_VALUE )
	{
		// File not found
		return false;
	}

	// File found

	// Release find handle
	FindClose( hFind );
	hFind = NULL;

	// The file exists
	return true;
}


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		_tprintf(_T("Fatal Error: MFC initialization failed\n"));
		nRetCode = 1;
	}
	else
	{
		// TODO: code your application's behavior here.
		FILE *in = _tfopen(argv[1], _T("r"));
		char temp[256] = {0,};
		//char buf[256] = {0,};
		CString path =  _T("C:\\Reposit_PDF\\PDF_PRO_NSERVER\\reg\\");
		CString fullpath = _T("");
		//CString imsi=_T("");

		while(fgets(temp, sizeof(temp)/sizeof(char), in) !=NULL)
		{
			size_t size = ::MultiByteToWideChar(CP_UTF8, 0, temp, -1, NULL, 0);
			wchar_t *wbuf = new wchar_t[size+1];
			::MultiByteToWideChar(CP_UTF8, 0, temp, -1, wbuf, size);
			//_tcscat(wbuf, _T(""));
			//imsi.Format(_T("%s"),temp); //wbuf
			CString imsi(wbuf);
			imsi.Trim();
			delete []wbuf;
			//_tcscpy(fullpath.GetBuffer(),temp);
			imsi.Append(_T(".pnidx"));
			//_tcscat(imsi.GetBuffer(), _T(".pnidx"));
			//_tcscpy(fullpath.GetBuffer(), path.GetBuffer());
			//_tcscat(fullpath.GetBuffer(),imsi.GetBuffer());
			fullpath = path + imsi;
			
			int a = _taccess_s(fullpath, 0);
			//bool a = FileExist(fullpath.GetBuffer());
			
			if(a)
			{
				FILE *out = _tfopen(fullpath.GetBuffer(), _T("w"));
				printf( "%s file opened\n",fullpath.GetBuffer() );
				fclose(out);
			}
			fullpath.ReleaseBuffer();
			memset(temp,0,sizeof(temp));
		}

		fclose(in);

	}

	return nRetCode;
}
